import com.alibaba.fastjson.{JSON, JSONObject}
import org.apache.spark.rdd.RDD
import org.apache.spark.sql.SQLContext
import org.apache.spark.{SparkConf, SparkContext}


/**
  * @ Auther: o2o-rd-0008
  * @ Date:   2019/12/9 15:13
  * @ Param:  ${PARAM}
  * @ Description: 
  */
object modify_cate_201911 {
  def main(args: Array[String]): Unit = {

    val conf = new SparkConf()
    conf.setAppName(s"${this.getClass.getSimpleName}")
    conf.set("spark.debug.maxToStringFields", "500")
    conf.setMaster("local[*]")
    conf.set("es.nodes", "192.168.1.157")
    conf.set("es.port", "9200")
    conf.set("cluster.name", "O2OElastic")
    conf.set("spark.serializer", "org.apache.spark.serializer.KryoSerializer")
    conf.set("spark.sql.caseSensitive", "true")
    val sc = new SparkContext(conf)
    val sqlContext = new SQLContext(sc)
    sc.setLogLevel("WARN")
    sc.hadoopConfiguration.set("fs.s3a.access.key", "GAO7EO9FWKPJ8WFCQDME")
    sc.hadoopConfiguration.set("fs.s3a.secret.key", "LZ0xaHBSYKHaJ9ECDbX9f7zin79UZkXfGoNapRPL")
    sc.hadoopConfiguration.set("fs.s3a.endpoint", "https://obs.cn-north-1.myhuaweicloud.com")

    import org.elasticsearch.spark._
    for (months <- 2 to 2){
      val value1: RDD[String] = sc.esJsonRDD(s"2020_tmall/tmall_2020_${months}",
        """
          |{
          |  "query": {
          |    "bool": {
          |      "should": [
          |        {
          |          "term": {
          |            "categoryId": {
          |              "value": "201302711"
          |            }
          |          }
          |        },{
          |          "term": {
          |            "categoryId": {
          |              "value": "201232102"
          |            }
          |          }
          |        },{
          |          "term": {
          |            "categoryId": {
          |              "value": "201306606"
          |            }
          |          }
          |        },{
          |          "term": {
          |            "categoryId": {
          |              "value": "201211201"
          |            }
          |          }
          |        },{
          |          "term": {
          |            "categoryId": {
          |              "value": "201292211"
          |            }
          |          }
          |        },{
          |          "term": {
          |            "categoryId": {
          |              "value": "201274970"
          |            }
          |          }
          |        },{
          |          "term": {
          |            "categoryId": {
          |              "value": "201215402"
          |            }
          |          }
          |        },{
          |          "term": {
          |            "categoryId": {
          |              "value": "201271673"
          |            }
          |          }
          |        }
          |      ]
          |    }
          |  }
          |}
        """.stripMargin).values
      val source_data = sqlContext.read.json(value1)
      source_data.createOrReplaceTempView("source_data")

      val mid = sqlContext.sql(
        s"""
           |select
           |*,
           |case
           |when categoryId='201302711' then '10025'
           |when categoryId='201232102' then '10015'
           |when categoryId='201306606' then '10022'
           |when categoryId='201211201' then '10025'
           |when categoryId='201292211' then '10015'
           |when categoryId='201274970' then '10014'
           |when categoryId='201215402' then '10017'
           |when categoryId='201271673' then '10014'
           |else firstCategoryId end as firstCategoryIds,
           |case
           |when categoryId='201302711' then '1002510'
           |when categoryId='201232102' then '1001506'
           |when categoryId='201306606' then '1002202'
           |when categoryId='201211201' then '1002507'
           |when categoryId='201292211' then '1001501'
           |when categoryId='201274970' then '1001402'
           |when categoryId='201215402' then '1001799'
           |when categoryId='201271673' then '1001402'
           |else secondCategoryId end as secondCategoryIds,
           |case
           |when categoryId='201302711' then '100251001'
           |when categoryId='201232102' then '100150699'
           |when categoryId='201306606' then '100220203'
           |when categoryId='201211201' then '100250799'
           |when categoryId='201292211' then '100150199'
           |when categoryId='201274970' then '100140201'
           |when categoryId='201215402' then '100179999'
           |when categoryId='201271673' then '100140202'
           |else thirdCategoryId end as thirdCategoryIds,
           |case
           |when categoryId='201302711' then '10025100199'
           |when categoryId='201232102' then '10015069999'
           |when categoryId='201306606' then '10022020399'
           |when categoryId='201211201' then '10025079999'
           |when categoryId='201292211' then '10015019999'
           |when categoryId='201274970' then '10014020199'
           |when categoryId='201215402' then '10017999999'
           |when categoryId='201271673' then '10014020299'
           |else fourthCategoryId end as fourthCategoryIds
           |from source_data
         """.stripMargin).drop("firstCategoryId","secondCategoryId","thirdCategoryId","fourthCategoryId")
        .withColumnRenamed("firstCategoryIds","firstCategoryId")
        .withColumnRenamed("secondCategoryIds","secondCategoryId")
        .withColumnRenamed("thirdCategoryIds","thirdCategoryId")
        .withColumnRenamed("fourthCategoryIds","fourthCategoryId")



      val result = mid.toJSON.rdd.map(line => {
        val lines = JSON.parseObject(line)
        val evaluates = lines.getOrDefault("evaluates", "-1").toString
        val Base_Info = lines.getOrDefault("Base_Info", "-1").toString
        var ev = "-1"
        var str = new JSONObject
        if (evaluates.equals("-1")) {
          ev = "{\"fuyi\":\"-1\"}"
        } else {
          str = JSON.parseObject(evaluates)
        }
        if (!ev.contains("fuyi")) {
          lines.put("evaluates", str)
        } else {
          val evs = JSON.parseObject(ev)
          lines.put("evaluates", evs)
        }
        lines.put("Base_Info",Base_Info)
        lines
      })
      result.saveToEs(s"2020_tmall/tmall_2020_${months}", Map("es.mapping.id" -> "good_id"))
    }





  }

}
